
/***
 * 判断用户滑动
 * 左滑还是右滑
 */
const getTouchData = (endX, endY, startX, startY) => {
  let turn = "";
  if (endX - startX > 50 && Math.abs(endY - startY) < 50) {      //右滑
    turn = "right";
  } else if (endX - startX < -50 && Math.abs(endY - startY) < 50) {   //左滑
    turn = "left";
  }
  return turn;
}

const changeadd = (that) => {
  if (that.data.num == 0) {
    that.setData({
      step1: false,
      step2: true,
      num: that.data.num + 1
    })
  }
  else if (that.data.num == 1) {
    that.setData({
      step2: false,
      step3: true,
      num: that.data.num + 1
    })
  }
  else if (that.data.num == 2) {
    that.setData({
      visible: true
    })
  }
}

const changereduce = (that) => {
  if (that.data.num == 2) {
    that.setData({
      step3: false,
      step2: true,
      num: that.data.num - 1
    })
  }
  else if (that.data.num == 1) {
    that.setData({
      step2: false,
      step1: true,
      num: that.data.num - 1
    })
  }
}

const checkform = (that) => {
  let describe = that.data.describe//描述
  let filename = that.data.filename//命名方式
  let value = that.data.value//标题
  let index = that.data.index//选择的文件类型
  if (value == '') {
    that.setData({
      OKtext: "标题未填写"
    })
    const action = that.data.actions2;
    action[0].loading = false;
    that.setData({
      actions2: action
    })
  }

  else if (describe == '') {
    that.setData({
      OKtext: "描述未填写"
    })
    const action = that.data.actions2;
    action[0].loading = false;
    that.setData({
      actions2: action
    })
  }
  else if (filename == '') {
    that.setData({
      OKtext: "命名方式未填写"
    })
    const action = that.data.actions2;
    action[0].loading = false;
    that.setData({
      actions2: action
    })
  }
  else if(that.data.datevalue==''||that.data.timevalue==''){
    that.setData({
      OKtext: "截止日期或时间未填写"
    })
    const action = that.data.actions2;
    action[0].loading = false;
    that.setData({
      actions2: action
    })
  }
  else {
    request(that)
  }

}

const request = (that) => {
  wx.getStorage({
    key: 'pwdWithSalt',
    success(res) {
      wx.request({
        url: 'https://localhost:8080/create_event',
        data: {
          groupnum: that.data.groupnum,
          title: that.data.value,
          describe: that.data.describe,
          filename: that.data.filename,
          index: that.data.index,
          pwdWithSalt: res.data,
          date:that.data.datevalue,
          time:that.data.timevalue
        },
        success(res) {
          console.log(res.data)
          if (res.data.errCode == 2019) {
            wx.showToast({
              title: '您不是这个小组的管理员，不能创建任务，即将自动返回...',
              icon: 'none',
              duration: 5000,
              mask: true
            })
            wx.navigateBack({
              delta: 1
            })
          }
          else if (res.data.errCode == 2020) {
            const action = that.data.actions2;
            action[0].loading = false;
            that.setData({
              actions2: action,
              visible: false,
              step1: false,
              step2: false,
              step3: false,
              step4: true,
              num: 3
            })
          }
          else{
            wx.showToast({
              title: '出现异常',
              icon: 'none',
              duration: 1000,
              mask: false
            })
          }
        }
      })
    }
  })
}

// pages/publish/publish.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    numList: [{
      name: '标题注释'
    }, {
      name: '文件类型'
    }, {
      name: '统一命名'
    }, {
      name: '完成发布'
    },],
    num: 0,
    step1: true,
    step2: false,
    step3: false,
    step4: false,
    shows: false, //控制下拉列表的显示隐藏，false隐藏、true显示
    array: ['图片(.jpg/png等)', '其他(.*)'],
    datevalue:'',
    timevalue:'',
    value: '',//title
    describe: '',
    filename: '',
    visible: false,
    index: 0,
    groupnum: '',
    actions2: [
      {
        name: '确定',
        color: '#000000'
      }
    ],
    OKtext: '',
    choosegrouping: true,
    array2: [{
      group_number: 'XXXXXXXXXXXXX',
      name: 'XXX',
      NbrMenbr: 0,
      current_activity: 0
    }, {
      group_number: 'XXXXXXXXXXXXX',
      name: 'XXX',
      NbrMenbr: 0,
      current_activity: 0
    }]
  },

  handleClickItem: function () {
    const action = this.data.actions2;
    action[0].loading = true;
    this.setData({
      actions2: action
    })
    checkform(this)
  },

  handleCancel: function () {
    this.setData({
      visible: false,
      OKtext: ''
    })
  },

  bindblurfilename: function (e) {
    this.setData({
      filename: e.detail.value
    })
  },

  myInput: function (event) {
    this.setData({
      value: event.detail.detail.value
    })
  },

  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },

  bindPickerChange_date: function (e) {
    this.setData({
      datevalue: e.detail.value
    })
  },

  bindPickerChange_time: function (e) {
    this.setData({
      timevalue: e.detail.value
    })
  },


  touchStart(e) {
    // console.log(e)
    this.setData({
      "touch.x": e.changedTouches[0].clientX,
      "touch.y": e.changedTouches[0].clientY
    });
  },

  touchEnd(e) {
    let x = e.changedTouches[0].clientX;
    let y = e.changedTouches[0].clientY;
    if (getTouchData(x, y, this.data.touch.x, this.data.touch.y) == "left") {
      changeadd(this)
    }
    else if (getTouchData(x, y, this.data.touch.x, this.data.touch.y) == "right") {
      changereduce(this)
    }
  },

  bindTextAreaBlur: function (e) {
    this.setData({
      describe: e.detail.value
    })
  },

  //选择要上传的上传文件

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getStorage({
      key: 'pwdWithSalt',
      success(res) {
        wx.request({
          url: 'https://localhost:8080/getGroupinfo/',
          data: {
            pwdWithSalt: res.data
          },
          success(res) {
            that.setData({
              array2: res.data
            })
          }
        })
      }
    });
  },

  Bselected: function (e) {
    this.setData({
      groupnum: e.currentTarget.dataset.item,
      choosegrouping: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})